package com.gem.domain;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 玲
 * @Description:
 * @create 2019-11-06 15:54
 * @Modified By:
 */
public class TestJson {
    public static void main(String[] args) {
        Map<String,  ClassInfo> map = new HashMap<>();
        List<ClassInfo> list = new ArrayList<>();
        ClassInfo classInfo = new ClassInfo();
        classInfo.setMName("村上春树");
        ClassInfo classInfo2 = new ClassInfo();
        classInfo.setMName("按时大苏打");
        ClassInfo classInfo3 = new ClassInfo();
        classInfo.setMName("啊大苏打");
        list.add(classInfo);
        list.add(classInfo2);
        list.add(classInfo3);
        map.put("class",classInfo);
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
//        System.out.println(json);
        HttpHeaders headers = new HttpHeaders();
        String httpPost = HttpPost(json, headers);
        System.out.println("main-->"+httpPost);
    }
    public static String HttpPost(String classInfo, HttpHeaders headers){
        String url = "http://10.40.1.34:8080/factory/TestJson";
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        RestTemplate rt = new RestTemplate();
        HttpEntity<String> httpEntity =new HttpEntity<>(classInfo);
        rt.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        ResponseEntity<String> res = rt.postForEntity(url, httpEntity, String.class);
        System.out.println("HttpPost-->"+res.getBody());
        return res.getBody();
    }
}
